<?php
/**
 * 《我的信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */

require_once(dirname(__FILE__) . '/../emp/Info.class.php');

class Resume extends Info {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
	}
	
	function layout($controller, $request) {
		$CategoryTree = array(
			'class'	  => 'Opened',		
				
			'个人简历' => array('cc'=>'resume.php?do=Resume.tab', 'js'=>''),
			'我的试卷' => array('cc'=>'resume.php?do=MyExam.my', 'js'=>''),			
		
			'退出' => 'index.php?do=Admin.logout',
		);
		
		$MenuTree = $this->tree($_SESSION['name'], $CategoryTree);
		$controller->assign('MenuTree', $MenuTree);

		// print_r($_SESSION['mytree']);

		$controller->display($request, 'resume/layout');
		return true;
	}
	
	function tree_my($v) {
		return true;
	}
	
	function tab($controller, $request) {
		$this->pretreat($controller);
		
		$data = array(		
			'基本信息' => array('cc'=>'resume.php?do=Resume.edit', 'js'=>''),
			
			'家庭关系' => array('cc'=>'resume.php?do=Family.find', 'js'=>''),
			'学习经历' => array('cc'=>'resume.php?do=Education.find', 'js'=>''),
			'工作经历' => array('cc'=>'resume.php?do=Work.find', 'js'=>''),	

			'简历预览' => array('cc'=>'resume.php?do=Resume.view', 'js'=>''),	
		);
		
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'resume/tab');
		return true;
	}
	
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'resume/edit');
		return true;
	}
		
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];

		$name = $request['name'];
		$engname = $request['engname'];

		$nationality = $request['nationality'];
		$education = $request['education'];

		$marriage = $request['marriage'];
		$mobile = $request['mobile'];
		$urgent = $request['urgent'];
		
		$jj = addslashes($request['jj']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s set engname = '%s', nationality = '%s', education = '%s', marriage = '%s', mobile = '%s', urgent = '%s', jj = '%s'";
		$fmt .= ", content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $engname, $nationality, $education, $marriage, $mobile, $urgent, $jj, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$row['dept_t'] = $this->_dept($row['dept']);

		$row['sex'] = $this->sex[$row['sex']];
		$row['education'] = $this->education[$row['education']];
		$row['marriage'] = $this->marriage[$row['marriage']];
		$row['nationality'] = $this->nationality[$row['nationality']];

		$controller->assign_by_ref('emp', $row);
		$controller->assign_by_ref('result', $row);
			
		$controller->assign_by_ref('family', $this->_my_additional(Constant::tbl_emp_family, $id));
		$controller->assign_by_ref('education', $this->_my_additional(Constant::tbl_emp_education, $id));
		$controller->assign_by_ref('work', $this->_my_additional(Constant::tbl_emp_work, $id));

		$controller->display($request, 'resume/view');
		return true;
	}
	
	function exchange($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);		
		$fmt = "SELECT * FROM %s WHERE type = '1' ORDER BY id";
		$sql = sprintf($fmt, Constant::tbl_tec, $id);		
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row['ok'] = true; 

			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', json_encode($data));
		$controller->display($request, 'my/exchange');
		return true;
	}

	/**
	 * 修改密码
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function password($controller, $request) {

		$controller->display($request, 'my/password');
		return true;
	}

	/**
	 * 修改密码
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function passwordPost($controller, $request) {
		$this->pretreat($controller);

		$id = $_SESSION['id'];
		
		$oldpassword = $request['password'];
		$password = $request['newpassword'];
		$repassword = $request['renewpassword'];
		
		$ret = array();

		$db = newdb('emp');
		$fmt = "SELECT * FROM %s WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $id);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			if ($this->_password($row['password'], $oldpassword)) {
				$ret['status'] = 2;
				echo json_encode($ret);
				return true;
			}
		}

		$fmt = "UPDATE %s SET password = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $password, $id);
		// debug($sql);
		$db->query($sql);

		$ret['status'] = 1;
		echo json_encode($ret);
		return true;
	}

	function _password($password, $input) {
		$s = trim($input);
		if (strlen($s) == 0) {
			return true;
		}

		if ($password == $input) {
			return false;
		}

		if ($password == md5($input)) {
			return false;
		}

		return true;
	}
	
	function notice_my($controller, $request) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s ORDER BY ID DESC LIMIT 2";
		$sql = sprintf($fmt, Constant::tbl_notice);
		debug($sql);
		$result = $db->query($sql);
		$data = $db->fetch_all($result);
		
		echo json_encode($data);
	}
	
	/**
	 * 修改密码
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function book($controller, $request) {

		$controller->display($request, 'my/book');
		return true;
	}
}
?>